class Node(object):
    def __init__(self,value=None):
        self.rchild = None
        self.lchild = None
        self.value = value

class Tree(object):
    def __init__(self):
        self.root =None
    def add(self,value):
        node = Node(value)
        queue = [self.root]
        if self.root == None:
            self.root =node
            return
        while queue:
            cur_node = queue.pop(0)
            if cur_node.lchild is None:
                cur_node.lchild = node
                return
            else:
                queue.append(cur_node.lchild)
            if cur_node.rchild is None:
                cur_node.rchild = node
                return
            else:
                queue.append(cur_node.rchild)
    def breath_travl(self):
        queue = [self.root]
        if self.root is None:
            return
        while queue:
            cur_node = queue.pop(0)
            print(cur_node.value,end=" ")
            if cur_node.lchild is not None:
                queue.append(cur_node.lchild)
            if cur_node.rchild is not None:
                queue.append(cur_node.rchild)

    def preorder(self,node):
        
        if node is None:
            return
        print(node.value,end=" ")
        self.preorder(node.lchild)
        self.preorder(node.rchild)
    def inorder(self,node):
        
        if node is None:
            return
        self.inorder(node.lchild)
        print(node.value,end=" ")
        self.inorder(node.rchild)
    def postorder(self,node):
        
        if node is None:
            return
        self.postorder(node.lchild)
        self.postorder(node.rchild)
        print(node.value,end=" ")

if __name__=="__main__":
    tree=Tree()
    tree.add(1)
    tree.add(2)
    tree.add(3)

    tree.add(4)
    tree.add(5)
    tree.add(6)
    tree.add(7)
    tree.breath_travl()
    print()
    tree.preorder(tree.root)
    print()

    tree.inorder(tree.root)
    print()

    tree.postorder(tree.root)

